package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.mapper.targetSystem.icon.IconDataMapper;
import com.xinchuang.entity.targetSystem.icon.IconData;
import com.xinchuang.service.targetSystem.IconDataService;
import com.xinchuang.util.SnowflakeIdWorker;
import com.xinchuang.service.targetSystem.IconDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IconDataServiceImpl
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-08-23 16:00
 * @Verion V1.0
 */
@Service
public class IconDataServiceImpl implements IconDataService {

    @Autowired
    private IconDataMapper iconDataMapper;

    private SnowflakeIdWorker snow = new SnowflakeIdWorker(0, 0);

    @Override
    public Result addIconData(String iconUrl) {
        IconData iconData = new IconData();
        iconData.setId(snow.nextId() + "");
        iconData.setIconUrl(iconUrl);
        iconData.setType(1);
        if (iconDataMapper.addIconData(iconData) > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error(500, "新增异常请稍等重试");
    }

    @Override
    public Result delIconData(String id) {
        if (iconDataMapper.delIconData(id) > 0) {
            return ResultUtil.success("删除成功", null);
        }
        return ResultUtil.error(500, "图片已被删除/不存在");
    }

    @Override
    public Result selectIconDataList(String id) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<IconData> iconData = iconDataMapper.selectIconDataList(1);
        if (CollectionUtil.isNotEmpty(iconData)) {
            Map<String, Object> map = null;
            for (IconData iconDatum : iconData) {
                map = new HashMap<>();
                map.put("id", iconDatum.getId());
                if (StringUtils.isNotBlank(id) && iconDatum.getId().equals(id)) {
                    map.put("pitch", true);
                } else {
                    map.put("pitch", false);
                }
                map.put("url", iconDatum.getIconUrl());
                resultList.add(map);
            }
        }
        return ResultUtil.success(resultList);
    }

    @Override
    public Result selectIconDataPull() {
        List<Map<String, Object>> list = iconDataMapper.selectIconDataPull(1);
        return ResultUtil.success(CollectionUtil.isNotEmpty(list) ? list : new ArrayList<>());
    }

}